package com.answer.service.impl;

import com.answer.dao.AnswerDao;
import com.answer.dto.ReturnError;
import com.answer.enums.AnswerEnum;
import com.answer.enums.ReturnInfoEnum;
import com.answer.exception.ElementEmptyException;
import com.answer.exception.RecordEmptyException;
import com.answer.model.Answer;
import com.answer.service.AnswerService;
import com.answer.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2017/4/13.
 */
@Service("answerService")
public class AnswerServiceImpl implements AnswerService {
    private static final Logger log = LoggerFactory.getLogger(AnswerServiceImpl.class);
    private final ReentrantLock lock = new ReentrantLock();
    @Autowired
    private AnswerDao answerDao;

    public Long countTotal(Map<String, Object> map) {
        return answerDao.countTotal(map);
    }

    public List<Answer> listByPage(Map<String, Object> map) {
        return answerDao.listByPage(map);
    }

    public List<Answer> listByMap(Map<String, Object> map) {
        return answerDao.listByPage(map);
    }

    public Answer getById(Long id) {
        return answerDao.getById(id);
    }

    public Answer getByMap(Map<String, Object> map) {
        return answerDao.getByMap(map);
    }

    @Transactional
    public ReturnError saveAns(Long id, Answer answer) throws ElementEmptyException {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            //选项为空
            if (StringUtil.isEmpty(answer.getOption())) {
                throw new ElementEmptyException(AnswerEnum.OPTION.getInfo());
            }
            //序号为空
//            if (StringUtil.isEmpty(answer.getSort().toString())) {
//                throw new ElementEmptyException(AnswerEnum.SORT.getInfo());
//            }
            map.put("qId", id);
            map.put("delFlag", 1);
            lock.lock();
            try {
                Long count = answerDao.countTotal(map);
                if (count >= 5) {
                    return new ReturnError(true, ReturnInfoEnum.FAIL.getType(), AnswerEnum.OUT.getInfo(), null);
                }
                answer.setQId(id);
                answer.setDelFlag(1);
                answer.setGmtCreate(new Timestamp(System.currentTimeMillis()));
                answer.setGmtModified(answer.getGmtCreate());
                int i = answerDao.saveBySort(answer);
                if (i != 0) {
                    return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), null);
                } else {
                    return new ReturnError(true, ReturnInfoEnum.FAIL.getType(), AnswerEnum.HAVE.getInfo(), null);
                }
            } finally {
                lock.unlock();
            }
        } catch (Exception e) {
            log.info(ReturnInfoEnum.ERROR.getInfo(), e);
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError updateAns(Answer ans) throws ElementEmptyException, RecordEmptyException {
        Answer answer = null;
        try {
            //选项为空
            if (StringUtil.isEmpty(ans.getOption())) {
                throw new ElementEmptyException(AnswerEnum.OPTION.getInfo());
            }
            //序号为空
//            if (StringUtil.isEmpty(ans.getSort().toString())) {
//                throw new ElementEmptyException(AnswerEnum.SORT.getInfo());
//            }
            answer = answerDao.getById(ans.getId());
            //记录不存在
            if (answer == null) {
                throw new RecordEmptyException(ReturnInfoEnum.NORECORD.getInfo());
            }
            answer.setOption(ans.getOption());
//            answer.setSort(ans.getSort());
            answer.setGmtModified(new Timestamp(System.currentTimeMillis()));
            int i = answerDao.update(answer);
            if (i != 0) {
                return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), null);
            } else {
                return new ReturnError(true, ReturnInfoEnum.FAIL.getType(), AnswerEnum.HAVE.getInfo(), null);
            }
        } catch (Exception e) {
            log.info(ReturnInfoEnum.ERROR.getInfo(), e);
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError removes(String ids) {
        String[] idarr = ids.split(",");
        try {
            for (String id : idarr) {
                answerDao.remove(Long.parseLong(id));
            }
            return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), null);
        } catch (Exception e) {
            log.info(ReturnInfoEnum.ERROR.getInfo(), e);
            throw new RuntimeException(e);
        }
    }

}
